package com.woniuxy.sddfp.access.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniuxy.sddfp.access.entity.TradeLog;
import com.woniuxy.sddfp.access.service.ITradeLogService;
import com.woniuxy.sddfp.common.model.dto.bank.BankCardAddDTO;
import com.woniuxy.sddfp.common.model.dto.bank.BankCardRequestDto;
import com.woniuxy.sddfp.common.model.dto.bank.SignatureDto;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 托管系统交易记录表 前端控制器
 * </p>
 *
 * @author yellow doctor
 * @since 2025-06-25
 */
@RestController
@RequestMapping("/trade-log")
public class TradeLogController {

    @Autowired
    private ITradeLogService tradeLogService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 绑卡
     */
    @PostMapping("/bank/access/card")
    public SignatureDto proxyBindCardLog(@RequestBody BankCardAddDTO bankCardAddDTO) {
        SignatureDto signatureDto = tradeLogService.proxyBindCardLog(bankCardAddDTO);
        return signatureDto;
    }

    /**
     * 银行托管系统回调接口
     */
    @PostMapping("/bank/access/receive")
    public String callbackUpdate(@RequestBody BankCardRequestDto bankCardRequestDto) {

        //01 修改日志     trade-log 中的  状态  setSynchronizeStatus ,  ConfirmDate
        tradeLogService.update(
                Wrappers.lambdaUpdate(TradeLog.class)
                      .set(TradeLog::getSynchronizeStatus,1)
                      .set(TradeLog::getConfirmDate,new Date())
                      .eq(TradeLog::getReqCode,bankCardRequestDto.getReqCode())
        );
        //02 发送数据到mq

        rabbitTemplate.convertAndSend(
                "sddfp-exchange-bind",
                "bind-card-message",
                bankCardRequestDto);

        return "mq的数据发送成功...";
    }

}
